package com.lancoo.klgcourseware.manager;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.lancoo.klgcourseware.entity.bean.KlgDbConstant;
import com.lancoo.klgcourseware.ui.activity.KlgCourseWareMainActivity;

/**
 * Created by pengmi on 2019/9/24 14:55
 * Description:知识点课件管理器
 */
public class KlgCourseWareManager {

    private static KlgCourseWareManager klgCourseWareManager;
    public static boolean hasVipLimit=false;
    public static String mediaUrl;
    private VipPayForCallBack vipPayForCallBack;
    private long time;
    private static int flag=1;
    public static final String SP_VIPPERMISS="sp_klg_vip";
    public static final String KEY_VIPPERMISS="key_klg_vip";
    public static final String KEY_MEDIAURL="key_klg_media_url";


    private KlgCourseWareManager() {

    }

    public static KlgCourseWareManager getInstance(){
        if(klgCourseWareManager==null){
            klgCourseWareManager=new KlgCourseWareManager();
        }
        return klgCourseWareManager;
    }

    public void setVipPayForCallBack(VipPayForCallBack vipPayForCallBack){
        this.vipPayForCallBack=vipPayForCallBack;
    }


    public void launchCourseWareKlg(Context mContext,String klgCode,boolean expandTrain){
        launchCourseWareKlg(mContext,klgCode,"",expandTrain);
    }

    public void launchCourseWareKlg(Context mContext,String klgCode,String url,boolean expandTrain){
        if(klgCode.split("&").length>0){
            klgCode=klgCode.split("&")[0];
        }
        launchCourseWareKlg(mContext,klgCode,url,false,expandTrain);
    }

    public void launchCourseWareKlg(Context mContext,String klgCode,String url,boolean haiVipLimit,boolean expandTrain){
        if(System.currentTimeMillis()-time<200){
            return;
        }
        time=System.currentTimeMillis();
        if(getVipLimitState(mContext)){
            showTrainAgainDialog(mContext);
            return;
        }
        if(!TextUtils.isEmpty(url)){
            KlgDbConstant.klg_baseUrl=url;
            SharedPreferences sharedPreferences = mContext.getSharedPreferences(KlgDbConstant.SP_KNOWLEDGE_COURSE, Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString(KlgDbConstant.SP_KNOWLEDGE_COURSE_URL, url);
            edit.commit();
        }
        KlgCourseWareMainActivity.launch(mContext,klgCode,expandTrain);
    }

    public void launchCourseWareKlg(Context mContext,String klgCode,String url,String mediaUrl,boolean haiVipLimit,boolean expandTrain){
        if(!TextUtils.isEmpty(mediaUrl)){
            if(TextUtils.equals("/",mediaUrl.substring(mediaUrl.length()-1))){
                KlgDbConstant.mediaUrl=mediaUrl;
            }else{
                KlgDbConstant.mediaUrl=mediaUrl+"/";
            }

        }else{
            KlgDbConstant.mediaUrl="";
        }
        launchCourseWareKlg(mContext,klgCode,url,haiVipLimit,expandTrain);
    }



    public void showTrainAgainDialog(Context mContext){
        if(vipPayForCallBack!=null){
            vipPayForCallBack.gotoVipPayPage(mContext);
        }
    }


    public interface VipPayForCallBack{
        void gotoVipPayPage(Context mContext);
    }

    /**
     * vip显示设置
     * @param mContext
     * @param vipLimit
     */
    public static void setVipLimitState(Context mContext,boolean vipLimit){
        if(mContext==null){
            return;
        }
        flag=0;
        hasVipLimit=vipLimit;
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(SP_VIPPERMISS, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean(KEY_VIPPERMISS,vipLimit);
        edit.commit();
    }



    public static boolean getVipLimitState(Context mContext){
        if(flag==0){
            return hasVipLimit;
        }else{
            if(mContext==null){
                return hasVipLimit;
            }else{
                flag=0;
                SharedPreferences sharedPreferences = mContext.getSharedPreferences(SP_VIPPERMISS, Context.MODE_PRIVATE);
                hasVipLimit=sharedPreferences.getBoolean(KEY_VIPPERMISS,false);
                return hasVipLimit;
            }
        }
    }

    public static void setMediaUrl(Context mContext,String url){
        mediaUrl=url;
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(SP_VIPPERMISS, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(KEY_MEDIAURL,mediaUrl);
        edit.commit();
        Log.e("KlgCourseWareManager","setMediaUrl,mediaUrl:"+mediaUrl);
    }

    /**
     * 获取音频播放的ip+port
     * @param mContext
     * @return
     */
    public static String getMediaUrl(Context mContext){
        if(TextUtils.isEmpty(mediaUrl)&&mContext!=null){
            SharedPreferences sharedPreferences = mContext.getSharedPreferences(SP_VIPPERMISS, Context.MODE_PRIVATE);
            mediaUrl=sharedPreferences.getString(KEY_MEDIAURL,mediaUrl);
        }else{

        }
        Log.e("KlgCourseWareManager","getMediaUrl,mediaUrl:"+mediaUrl);
        return mediaUrl;
    }

    /**
     * 备考专用
     * @param mContext
     * @param klgCode
     * @param url
     * @param mediaUrl
     * @param expandTrain
     */
    public void launchCourseWareKlg(Context mContext,String klgCode,String url,String mediaUrl,boolean expandTrain,
                                    String trainLevel){
        if(!TextUtils.isEmpty(mediaUrl)){
            if(TextUtils.equals("/",mediaUrl.substring(mediaUrl.length()-1))){
                KlgDbConstant.mediaUrl=mediaUrl;
            }else{
                KlgDbConstant.mediaUrl=mediaUrl+"/";
            }

        }else{
            KlgDbConstant.mediaUrl="";
        }
        setTrainLevel(mContext,trainLevel);
        launchCourseWareKlg(mContext,klgCode,url,expandTrain);
    }


    private void setTrainLevel(Context mContext,String trainLevel){
        //设置训练等级
        int trainType=-1;
        if(!TextUtils.isEmpty(trainLevel)){
            switch (trainLevel){
                case "A"://小学
                    trainType=0;
                    break;
                case "B"://初中
                    trainType=0;
                    break;
                case "C"://高中
                    trainType=1;
                    break;
                case "D"://中职
                    trainType=0;
                    break;
                case "E"://高职
                    trainType=1;
                    break;
                case "F": //大学公共英语
                    trainType=2;
                    break;
                case "H"://大学英语专业
                    trainType=2;
                    break;
                case "J"://研究生公共英语
                    trainType=2;
                    break;
                default:
                    trainType=-1;
            }
        }
        SharedPreferences sharedPreferences=mContext.getSharedPreferences(KlgDbConstant.SP_KNOWLEDGE_COURSE,Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putInt(KlgDbConstant.SP_TRAIN_LEVEL,trainType);
        edit.commit();
    }

}
